#! /bin/sh
#
# This shell script executes ``$1 --info-all'', and in addition
# it prints other useful information, such as the current date,
# load average, and user running the benchmark
#

me=`echo "$0" | sed -e 's,.*/,,'`

usage="\
Usage: $0 [OPTION] program

  --time-min=X       set minimum measurement time
  -h, --help         print this help, then exit
"

help="
Try \`$me --help' for more information."

verify="no"
maxn=1048576
time_min=""

prev=""
for option
do

  # If the previous option needs an argument, assign it.
  if test -n "$prev"; then
    shift
    eval "$prev=\$option"
    prev=
    continue
  fi

  optarg=`expr "x$option" : 'x[^=]*=\(.*\)'`

  case $option in

    --help | --h* | -h ) echo "$usage"; exit 0 ;;
    --time-min) prev=time_min; shift;;
    --time-min=*) time_min=$optarg; shift;;
    -- ) break ;; # Stop option processing
    -* )
       echo "$me: invalid option $1$help"
       exit 1 ;;

    * )
       break ;;
  esac
done

if test -n "$prev"; then
  option=--`echo $prev | sed 's/_/-/g'`
  echo "$me: error: missing argument to $option" >&2
  exit 1
fi

case $# in
 0) echo "$me: missing argument$help" >&2
    exit 1;;
 1) ;;
 *) echo "$me: too many arguments$help" >&2
    exit 1;;
esac

program="$1"

if test ! -x "$program"; then
    echo "``$program'' is not executable!" >&2
    exit 1
fi

if test -n "$USER"; then 
   user="$USER"
elif test -n "$LOGNAME"; then
   user="$LOGNAME";
else
   user=`whoami`
fi;

test -n "$time_min" || time_min=`$program --print-time-min`
lav=`(uptime || w)  2>/dev/null | sed 1q`
hostname=`(hostname || uname -n) 2>/dev/null | sed 1q`

echo "("
$program --info-all
echo "(time-min ${time_min})"
echo "(benchmark-date \"`date`\")"
echo "(benchmark-user \"${user}\")"
echo "(load-average \"${lav}\")"
echo "(hostname \"${hostname}\")"
echo "(progname \"${program}\")"
echo ")"